﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using CoderModelNS;
using System.Threading;

namespace MyCoderGui
{
    public partial class Form1 : Form
    {
        private CoderModel encoder;
        //private static string homeDir = @"d:\coder\";
        private static string homeDir = Application.StartupPath + "\\";
        public delegate void PrintToUI(string str);


        bool encoderDirty;

        StringBuilder txtBuffer = new StringBuilder();
        Thread printThread;

        public Form1()
        {
            InitializeComponent();
            comboBoxCRF.SelectedIndex = 23;
            comboBoxAACBR.SelectedIndex = 3;
            cbMethod.SelectedIndex = 4;
            encoderDirty = true;
            cbResolution.SelectedIndex = 0;
            printThread = new Thread(new ThreadStart(printThreadFun));
            printThread.Start();
        }

        private void selectInputFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Multiselect = false;
            openDlg.ShowDialog();
            inputFileTxt.Text = openDlg.FileName;
        }

        private void chooseOutputFileButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.AddExtension = true;
            saveDlg.DefaultExt = ".mp4";
            saveDlg.Filter = "MP4 Files (*.mp4)|*.mp4";
            saveDlg.ShowDialog();
            outputFileTxt.Text = saveDlg.FileName;
        }

        private void buttonInitData_Click(object sender, EventArgs e)
        {
            if (inputFileTxt.Text == "" || !File.Exists(inputFileTxt.Text))
            {
                MessageBox.Show("Please select input file");
                return;
            }
            if (outputFileTxt.Text == "" || !(Directory.Exists(Path.GetDirectoryName(outputFileTxt.Text)) && Path.GetFileName(outputFileTxt.Text) != "")) {
                MessageBox.Show("Please select output file");
                return;
            }
            encoder = new CoderModel(homeDir, inputFileTxt.Text, outputFileTxt.Text);
            encoder.outputHandler += new CoderModel.OutputHandler(printInfomation);
            encoder.initCoderModel((String)comboBoxCRF.SelectedItem, (String)comboBoxAACBR.SelectedItem,cbMethod.SelectedIndex, cbResolution.SelectedIndex);
            
            
            textBoxMencoderA.Text = encoder.VideoCoder.ArgsStr;
            textBoxAacA.Text = encoder.AudioCoder.AacEncArgs;
            textBoxMp4A.Text = encoder.Mp4Muxer.AudioArgs;
            textBoxMp4V.Text = encoder.Mp4Muxer.VideoArgs;

            encoderDirty = false;
            //MessageBox.Show(Directory.GetCurrentDirectory());
        }

        



        private void printThreadFun() {
            string tempstr;
            while (true)
            {
                if (txtBuffer.ToString() != "")
                {

                    lock (txtBuffer)
                    {
                        tempstr = txtBuffer.ToString();	                    
                        txtBuffer.Clear();
                    }
                    if (textBoxConsole.InvokeRequired == true)
                    {
                        textBoxConsole.Invoke(new PrintToUI(textBoxConsole.AppendText), new object[] { tempstr });
                    }
                    else
                    {
                        textBoxConsole.AppendText(tempstr);
                    }
                        
                }
                Thread.Sleep(100);
            }
        }

        private void printInfomation(string infoStr) {
            lock (txtBuffer) {
                txtBuffer.AppendLine(infoStr);
            }
                        
            //textBoxConsole.AppendText(infoStr);
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (encoderDirty) {
                MessageBox.Show("The coding information is not valid!! Please update command line arguments first!!");
                return;
            }
            encoder.VideoCoder.ArgsStr = textBoxMencoderA.Text;
            encoder.AudioCoder.AacEncArgs = textBoxAacA.Text;
            encoder.Mp4Muxer.VideoArgs = textBoxMp4V.Text;
            encoder.Mp4Muxer.AudioArgs = textBoxMp4A.Text;

            Thread codingTh = new Thread(new ThreadStart(encoder.startCoding));
            codingTh.Start();
        }

        private void cbMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            encoderDirty = true;
        }

        private void comboBoxAACBR_SelectedIndexChanged(object sender, EventArgs e)
        {
            encoderDirty = true;
        }

        private void comboBoxCRF_SelectedIndexChanged(object sender, EventArgs e)
        {
            encoderDirty = true;
        }

        private void inputFileTxt_TextChanged(object sender, EventArgs e)
        {
            encoderDirty = true;
        }

        private void outputFileTxt_TextChanged(object sender, EventArgs e)
        {
            encoderDirty = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (encoder != null) {
                encoder.VideoCoder.killProcess();
            }
            printThread.Abort();
        }



        
    }
}
